<?php 
namespace app\Admin\controller;
use think\Controller;
use think\Db;
use think\Cookie;
class Admin extends Base
{
	//管理员列表
	public function admin_list(){
		$data = Db('user')->where('is_show=1')->select();
        $this->assign(['data'=>$data]);
		return $this->fetch();
	}

	public function admin_add(){
		return $this->fetch();
	}
	//管理员添加
	public function admin_adds()
	{
		if(request()->isPost()){
//		dump(input('post.')); //打印接收到的参数
 
			$data = [
				'user_name'=>input('user_name'),
    			'user_pwd'=>input('user_pwd'),
    			'user_email'=>input('user_email'),
    			'add_time'=>date("Y-m-d H:i:s"),
			];
			
		/*	Db('表名') 数据库助手函数*/
			if(Db('user') -> insert($data)){		//添加数据
				return $this->success('添加成功','admin_list');
			}else{
				return $this->error('添加管理员失败');
			}
		}
		return;

	}

	//删除
	public function admin_del()
	{
		$user_id = input('get.user_id');
		// var_dump($user_id);die;
		// $res = Db('user')->where('user_id',$user_id)->data(['is_show'=>0])->update();
		$res =  Db('user')->where('user_id',$user_id)->setField('is_show',0);
   		if ($res) {
   			return $this->success('删除成功','admin_list');
   		}else{
   			return $this->error('删除失败');
   		}

	}
//修改默认值
	public function admin_up(){
		return $this->fetch();
	}
//修改
	public function admin_ups()
	{
		$userid=Cookie::get('userid');
		// $aa=$_COOKIE['userid'];
		// echo $aa;
		// var_dump($userid);die;
		
		$u_pwd=input('post.old_password');
		$user_pwds=input('post.user_pwd');
		if(empty($u_pwd)){
			echo "<script>alert('请输入原密码');history.go(-1)</script>";
		}else{
			$res= Db('user')->where('user_id',$userid)->select();
			// print_r($res);die;
			$user_pwd=$res[0]['user_pwd'];
			if($u_pwd!=$user_pwd){
		   		echo "<script>alert('密码与原密码不一致');history.go(-1)</script>";exit;
        	}
        	if(empty($user_pwds)){
        		echo "<script>alert('密码不能为空');history.go(-1)</script>";exit;
        	}
        	$res = Db('user')->where('user_id',$userid)->update(['user_pwd'=>$user_pwds]);
        	if($res){
				echo "<script>alert('密码修改成功,请重新登录');location.href='/admin/login/login'</script>";
			}else{
				echo "<script>alert('密码修改失败')</script>";
			}
		}

	}


	public function admin_allot(){
		$user_id=input('get.user_id');
		$data = Db('role')->select();
		$this->assign(['data'=>$data]);
		$this->assign(['user_id'=>$user_id]);
		return $this->fetch();
	}
	//管理员分配角色
}//结尾